package com.api.gbhg.liuwenbo.version2.api;

import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.version2.pojo.BudgetSectionIncome2;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.*;

/**
 * 财政拨款2
 */
@Path("gb/FinancialAppropriation2Api")
public class FinancialAppropriation2Api {

    //人员经费
    private static Map<String,Object> ryjfMap ;
    //公用经费
    private static Map<String,Object> gyjfMap ;
    //项目经费
    private static Map<String,Object> xmjfMap ;
    private static Object[][] xmjfArr;
    static {
        ryjfMap = new HashMap<>();
        ryjfMap.put("2010901","行政运行-人员经费");
        ryjfMap.put("2041001","缉私警察－行政运行-人员经费");
        ryjfMap.put("2080501","行政单位离退休");
        ryjfMap.put("2080505","机关事业单位基本养老保险缴费支出");
        ryjfMap.put("2080506","机关事业单位职业年金缴费支出");
        ryjfMap.put("2101101","行政单位医疗");
        ryjfMap.put("2101103","公务员医疗补助");
        ryjfMap.put("2210201","住房公积金");
        ryjfMap.put("2210203","购房补贴");
        ryjfMap.put("2010903","机关服务");
        ryjfMap.put("2010950","事业运行");

        gyjfMap = new HashMap<>();
        gyjfMap.put("2010901","行政运行-公用经费");
        gyjfMap.put("2041001","缉私警察－行政运行-公用经费");
        gyjfMap.put("2080501","行政单位离退休");
        gyjfMap.put("2010903","机关服务");
        gyjfMap.put("2010950","事业运行");

        xmjfArr = new Object[][]{
                {"2010902", "一般行政管理事务", Arrays.asList("海关综合业务管理专项", "海关国际交流与合作", "中央基建投资")},
                {"2010905", "缉私办案", "海关缉私办案费"},
                {"2010907", "口岸管理", "中国电子口岸建设运行经费"},
                {"2010908", "信息化建设", "信息化运行维护（直接合并这个所有的科目下级）"},
                {"2010909", "海关关务", Arrays.asList("海关执法保障及能力提升专项", "海关系统基建支出", "资产运行维护")},
                {"2010910", "关税征管", "海关关税征管业务费"},
                {"2010911", "海关监管", "海关监管专项"},
                {"2010912", "检验检疫", "海关检验检疫专项"},
                {"2041002", "一般行政管理事务", "资产运行维护"},
                {"2041006", "信息化建设", "信息化运行维护"},
                {"2041007", "缉私业务", "打击走私犯罪业务费"}};
    }
    private Map<String,Object> msgMap ;

    @POST
    @Path("/getDataList")
    @Produces(MediaType.APPLICATION_JSON)
    public String getDataList(Map<String,Object> param){
        msgMap = new HashMap<>();

        //检查参数是否合法
        if (!param.containsKey("nf") || !param.containsKey("jz")){
            msgMap.put("code",204);
            msgMap.put("msg","非法参数！");
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }
        try {
            Map<String, Object> data = getData(param.get("nf").toString(), param.get("jz").toString());
            msgMap.put("code",200);
            msgMap.put("msg","success");
            msgMap.put("data",data);
        } catch (Exception e) {
            e.printStackTrace();
            msgMap.put("code",500);
            msgMap.put("msg","程序异常:"+e.toString());
            msgMap.put("data","");
            return JSON.toJSONString(msgMap);
        }
        return JSON.toJSONString(msgMap);
    }

    /**
     * 获取数据
     * @param nf
     *          年份
     * @param jz
     *          结转字段名
     * @return
     */
    public Map<String,Object> getData(String nf,String jz) throws Exception{
        Map<String,Object> map = new HashMap<>();

        //人员经费数据
        List<BudgetSectionIncome2> ryjfList = new LinkedList<>();
        for (Map.Entry<String, Object> entry : ryjfMap.entrySet()) {
            BudgetSectionIncome2 ryjf = getJfata(nf, jz,entry.getKey(),0);
            ryjfList.add(ryjf);
        }
        map.put("0",ryjfList);

        //公用经费数据
        List<BudgetSectionIncome2> gyjfList = new LinkedList<>();
        for (Map.Entry<String, Object> entry : gyjfMap.entrySet()) {
            BudgetSectionIncome2 gyjf = getJfata(nf, jz,entry.getKey(),1);
            gyjfList.add(gyjf);
        }
        map.put("1",gyjfList);

        //项目经费数据
        List<BudgetSectionIncome2> xmjfList = new LinkedList<>();
        for (int i = 0; i < xmjfArr.length; i++) {
            BudgetSectionIncome2 xmjf = getXmjfData(nf, jz, xmjfArr[i]);
            xmjfList.add(xmjf);
        }
        map.put("2",xmjfList);
        return map;
    }

    public BudgetSectionIncome2 getJfata(String nf, String jz, String id, Integer srzxId){
        BudgetSectionIncome2 bean = getBudgetSectionIncome2();

        bean.setName(id);//编号
        //srzx 0:人员经费 1:公用经费 2:项目经费
        String srzx = "";
        if (srzxId==0){
            srzx = "人员经费";
            bean.setKmmc(ryjfMap.get(id).toString());//科目名称
        }else if(srzxId==1){
            srzx = "公用经费";
            bean.setKmmc(gyjfMap.get(id).toString());//科目名称
        }

        RecordSet rs = new RecordSet();
        String sql = "select * from (select id,substring(gnkmmc,2,9) gnkmmc,kmdm,"+jz+",yjsr,snjzhpje,yhpje,yqndyjjzje,dw from uf_zyczbksr_km where srnf = "+nf+")kmb where gnkmmc in ("+ id +")";
        rs.executeQuery(sql);
        List<String> idList = new LinkedList<>();
        while (rs.next()){
            idList.add(rs.getString("id"));
            //接下来需要累加最末级金额
            List<BudgetSectionIncome2> xjList = getCzbksrEjData(rs.getString("id"), jz,rs.getString("dw"),nf,srzx);
            if (xjList.size()>0){
                BigDecimal snjz = BigDecimal.ZERO;//上年结转
                BigDecimal bnsr = BigDecimal.ZERO;//本年收入
                BigDecimal snjzysap = BigDecimal.ZERO;//上年结转预算安排
                BigDecimal bnsrysap = BigDecimal.ZERO;//本年收入预算安排
                BigDecimal snjzwap = BigDecimal.ZERO; //上年结转未安排
                BigDecimal bnsrwap = BigDecimal.ZERO; //本年收入未安排
                BigDecimal snjzyszc = BigDecimal.ZERO; //上年结转预算支出
                BigDecimal bnsryszc = BigDecimal.ZERO; //本年收入预算支出
                BigDecimal snjzyszxl = BigDecimal.ZERO; //上年结转预算执行率
                BigDecimal bnsryszxl = BigDecimal.ZERO; //本年收入预算执行率
                List<String> dataidBj = new ArrayList<>();//记录本级
                List<String> dataidXj = new ArrayList<>();//记录下级
                for (BudgetSectionIncome2 beans:xjList){
                    dataidBj.add(beans.getId());
                    if (beans.getDataid() != null && !beans.getDataid().isEmpty()){
                        dataidXj.add(beans.getDataid());
                    }

                    snjz = snjz.add(new BigDecimal(beans.getSnjz()));
                    bnsr = bnsr.add(new BigDecimal(beans.getBnsr()));

                    snjzysap = snjzysap.add(new BigDecimal(beans.getSnjzysap()));
                    bnsrysap = bnsrysap.add(new BigDecimal(beans.getBnsrysap()));

                    snjzwap = snjzwap.add(new BigDecimal(beans.getSnjzwap()));
                    bnsrwap = bnsrwap.add(new BigDecimal(beans.getBnsrwap()));

                    snjzyszc = snjzyszc.add(new BigDecimal(beans.getSnjzyszc()));
                    bnsryszc = bnsryszc.add(new BigDecimal(beans.getBnsryszc()));

                    snjzyszxl = snjzyszxl.add(new BigDecimal(beans.getSnjzyszxl()));
                    bnsryszxl = bnsryszxl.add(new BigDecimal(beans.getBnsryszxl()));
                }
                String dataid = "";
                if (dataidXj.size()>0){
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",")+","+VersionFlowUtil.joinStringBySplit(dataidXj,",");
                }else{
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",");
                }
                bean.setDataid(dataid);

                bean.setSnjz(new BigDecimal(bean.getSnjz()).add(snjz).toString());
                bean.setBnsr(new BigDecimal(bean.getBnsr()).add(bnsr).toString());
                bean.setHj(String.valueOf(new BigDecimal(bean.getSnjz()).add(new BigDecimal(bean.getBnsr()))));

                bean.setSnjzysap(new BigDecimal(bean.getSnjzysap()).add(snjzysap).toString());
                bean.setBnsrysap(new BigDecimal(bean.getBnsrysap()).add(bnsrysap).toString());
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));

                bean.setSnjzwap(new BigDecimal(bean.getSnjzwap()).add(snjzwap).toString());
                bean.setBnsrwap(new BigDecimal(bean.getBnsrwap()).add(bnsrwap).toString());
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

                bean.setSnjzyszc(new BigDecimal(bean.getSnjzyszc()).add(snjzyszc).toString());
                bean.setBnsryszc(new BigDecimal(bean.getBnsryszc()).add(bnsryszc).toString());
                bean.setHj3(String.valueOf(new BigDecimal(bean.getSnjzyszc()).add(new BigDecimal(bean.getBnsryszc()))));

                if (snjzyszc.compareTo(new BigDecimal("0"))>0){
                    bean.setSnjzyszxl((snjzysap).divide(snjzyszc,BigDecimal.ROUND_CEILING).toString());
                }
                if (bnsryszc.compareTo(new BigDecimal("0"))>0){
                    bean.setBnsryszxl((bnsrysap).divide(bnsryszc,BigDecimal.ROUND_CEILING).toString());
                }
                if (Double.valueOf(bean.getHj3())>0){
                    bean.setHj4(new BigDecimal(bean.getHj2()).divide(new BigDecimal(bean.getHj3()),BigDecimal.ROUND_CEILING).toString());
                }
            }else{
                //已到最下级  取自身值
                String snjz = new BigDecimal(bean.getSnjz()).add(new BigDecimal(rs.getString(jz))).toString();
                bean.setSnjz(snjz);
                String yjsr = new BigDecimal(bean.getSnjz()).add(new BigDecimal(rs.getString("yjsr"))).toString();
                bean.setBnsr(yjsr);
                bean.setHj(String.valueOf(new BigDecimal(snjz).add(new BigDecimal(yjsr))));

                bean.setSnjzysap(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(rs.getString("snjzhpje"))).toString());
                bean.setBnsrysap(new BigDecimal(bean.getBnsrysap()).add(new BigDecimal(rs.getString("yhpje"))).toString());
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));

                bean.setSnjzwap(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(rs.getString("yqndyjjzje"))).toString());
                bean.setBnsrwap(new BigDecimal(bean.getBnsrwap()).add(new BigDecimal(rs.getString("yjsr"))).toString());
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

                RecordSet tempRs = new RecordSet();
                String km = new BaseBean().getPropValue("Verification","yj")+"_"+rs.getString("id");
                BigDecimal snbxje = getSjbxje(tempRs, "上年", "0", "", "", km);
                bean.setSnjzyszc(snbxje.toString());
                BigDecimal bnbxje = getSjbxje(tempRs, "当年", "0", "", "", km);
                bean.setBnsryszc(bnbxje.toString());
                bean.setHj3(String.valueOf(snbxje.add(bnbxje)));

                BigDecimal snjzysap = new BigDecimal(bean.getSnjzysap());
                if (snbxje.compareTo(new BigDecimal("0"))>0&&snjzysap.compareTo(new BigDecimal("0"))>0){
                    bean.setSnjzyszxl(snjzysap.divide(snbxje,BigDecimal.ROUND_CEILING).toString());
                }
                BigDecimal bnsrysap = new BigDecimal(bean.getBnsrysap());
                if (bnbxje.compareTo(new BigDecimal("0"))>0&&bnsrysap.compareTo(new BigDecimal("0"))>0){
                    bean.setBnsryszxl(bnsrysap.divide(bnbxje,BigDecimal.ROUND_CEILING).toString());
                }
                BigDecimal hj2 = new BigDecimal(bean.getHj2());
                if (Double.valueOf(bean.getHj3())>0&&hj2.compareTo(new BigDecimal("0"))>0){
                    bean.setHj4(hj2.divide(new BigDecimal(bean.getHj3()),BigDecimal.ROUND_CEILING).toString());
                }
            }
        }
        bean.setId(VersionFlowUtil.joinStringBySplit(idList,","));
        return bean;
    }

    /**
     * 获取中央科目二级数据
     * @param kmId
     * @return
     */
    public List<BudgetSectionIncome2> getCzbksrEjData(String kmId, String jzField,String dw,String nf,String srzx){
        RecordSet rs = new RecordSet();
        String sql = "select id,srzx,"+jzField+",yjsr,snjzhpje,yhpje,yqndyjjzje,yjsr from uf_zyczbksr_xj where szgnkm = "+kmId+" and (sjzxsr is null or sjzxsr='')  ";
        if (!srzx.isEmpty()){
            sql +=" and srzx = '"+srzx+"' ";
        }
        rs.executeQuery(sql);
        List<BudgetSectionIncome2> list = new ArrayList<>();

        while (rs.next()){
            BudgetSectionIncome2  bean = getBudgetSectionIncome2();
            bean.setId(rs.getString("id"));
            bean.setKmjxj(1);
            bean.setKmmc(rs.getString("srzx"));
            List<BudgetSectionIncome2> xjList = recursionXj(rs.getString("id"), jzField,dw,nf);
            if (xjList.size()>0){
                BigDecimal snjz = BigDecimal.ZERO;//上年结转
                BigDecimal bnsr = BigDecimal.ZERO;//本年收入
                BigDecimal snjzysap = BigDecimal.ZERO;//上年结转预算安排
                BigDecimal bnsrysap = BigDecimal.ZERO;//本年收入预算安排
                BigDecimal snjzwap = BigDecimal.ZERO; //上年结转未安排
                BigDecimal bnsrwap = BigDecimal.ZERO; //本年收入未安排
                BigDecimal snjzyszc = BigDecimal.ZERO; //上年结转预算支出
                BigDecimal bnsryszc = BigDecimal.ZERO; //本年收入预算支出
                BigDecimal snjzyszxl = BigDecimal.ZERO; //上年结转预算执行率
                BigDecimal bnsryszxl = BigDecimal.ZERO; //本年收入预算执行率
                List<String> dataidBj = new ArrayList<>();//记录本级
                List<String> dataidXj = new ArrayList<>();//记录下级
                for (BudgetSectionIncome2 beans:xjList){
                    dataidBj.add(beans.getId());
                    if (beans.getDataid() != null && !beans.getDataid().isEmpty()){
                        dataidXj.add(beans.getDataid());
                    }
                    snjz = snjz.add(new BigDecimal(beans.getSnjz()));
                    bnsr = bnsr.add(new BigDecimal(beans.getBnsr()));

                    snjzysap = snjzysap.add(new BigDecimal(beans.getSnjzysap()));
                    bnsrysap = bnsrysap.add(new BigDecimal(beans.getBnsrysap()));

                    snjzwap = snjzwap.add(new BigDecimal(beans.getSnjzwap()));
                    bnsrwap = bnsrwap.add(new BigDecimal(beans.getBnsrwap()));

                    snjzyszc = snjzyszc.add(new BigDecimal(beans.getSnjzyszc()));
                    bnsryszc = bnsryszc.add(new BigDecimal(beans.getBnsryszc()));

                    snjzyszxl = snjzyszxl.add(new BigDecimal(beans.getSnjzyszxl()));
                    bnsryszxl = bnsryszxl.add(new BigDecimal(beans.getBnsryszxl()));
                }
                String dataid = "";
                if (dataidXj.size()>0){
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",")+","+VersionFlowUtil.joinStringBySplit(dataidXj,",");
                }else{
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",");
                }
                bean.setDataid(dataid);

                bean.setSnjz(snjz.toString());
                bean.setBnsr(bnsr.toString());
                bean.setHj(snjz.add(bnsr).toString());

                bean.setSnjzysap(snjzysap.toString());
                bean.setBnsrysap(bnsrysap.toString());
                bean.setHj1(snjzysap.add(bnsrysap).toString());

                bean.setSnjzwap(snjzwap.toString());
                bean.setBnsrwap(bnsrwap.toString());
                bean.setHj2(snjzwap.add(bnsrwap).toString());

                bean.setSnjzyszc(snjzyszc.toString());
                bean.setBnsryszc(bnsryszc.toString());
                bean.setHj3(snjzyszc.add(bnsryszc).toString());

                if (snjzyszc.compareTo(new BigDecimal("0"))>0){
                    bean.setSnjzyszxl((snjzysap).divide(snjzyszc,BigDecimal.ROUND_CEILING).toString());
                }
                if (bnsryszc.compareTo(new BigDecimal("0"))>0){
                    bean.setBnsryszxl((bnsrysap).divide(bnsryszc,BigDecimal.ROUND_CEILING).toString());
                }
                if (Double.valueOf(bean.getHj3())>0){
                    bean.setHj4(new BigDecimal(bean.getHj2()).divide(new BigDecimal(bean.getHj3()),BigDecimal.ROUND_CEILING).toString());
                }
            }else{
                //已到最下级  取自身值
                String snjz = new BigDecimal(bean.getSnjz()).add(new BigDecimal(rs.getString(jzField))).toString();
                bean.setSnjz(snjz);
                String yjsr = new BigDecimal(bean.getSnjz()).add(new BigDecimal(rs.getString("yjsr"))).toString();
                bean.setBnsr(yjsr);
                bean.setHj(String.valueOf(new BigDecimal(snjz).add(new BigDecimal(yjsr))));

                bean.setSnjzysap(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(rs.getString("snjzhpje"))).toString());
                bean.setBnsrysap(new BigDecimal(bean.getBnsrysap()).add(new BigDecimal(rs.getString("yhpje"))).toString());
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));

                bean.setSnjzwap(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(rs.getString("yqndyjjzje"))).toString());
                bean.setBnsrwap(new BigDecimal(bean.getBnsrwap()).add(new BigDecimal(rs.getString("yjsr"))).toString());
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

                RecordSet tempRs = new RecordSet();
                String xj = new BaseBean().getPropValue("Verification","xj")+"_"+rs.getString("id");
                BigDecimal snbxje = getSjbxje(tempRs, "上年", "0", "", "", xj);
                bean.setSnjzyszc(snbxje.toString());
                BigDecimal bnbxje = getSjbxje(tempRs, "当年", "0", "", "", xj);
                bean.setBnsryszc(bnbxje.toString());
                bean.setHj3(String.valueOf(snbxje.add(bnbxje)));

                BigDecimal snjzysap = new BigDecimal(bean.getSnjzysap());
                if (snbxje.compareTo(new BigDecimal("0"))>0&&snjzysap.compareTo(new BigDecimal("0"))>0){
                    bean.setSnjzyszxl(snjzysap.divide(snbxje,BigDecimal.ROUND_CEILING).toString());
                }
                BigDecimal bnsrysap = new BigDecimal(bean.getBnsrysap());
                if (bnbxje.compareTo(new BigDecimal("0"))>0&&bnsrysap.compareTo(new BigDecimal("0"))>0){
                    bean.setBnsryszxl(bnsrysap.divide(bnbxje,BigDecimal.ROUND_CEILING).toString());
                }
                BigDecimal hj2 = new BigDecimal(bean.getHj2());
                if (Double.valueOf(bean.getHj3())>0&&hj2.compareTo(new BigDecimal("0"))>0){
                    bean.setHj4(hj2.divide(new BigDecimal(bean.getHj3()),BigDecimal.ROUND_CEILING).toString());
                }
            }
            list.add(bean);
        }
        return list;
    }

    /**
     * 获取下级数据合 涉及递归
     * @param id
     * @param jzField
     * @return
     */
    public List<BudgetSectionIncome2> recursionXj(String id,String jzField,String dw,String nf){
        RecordSet rs = new RecordSet();
        String sql = "select id,srzx,"+jzField+",yjsr,snjzhpje,yhpje,yqndyjjzje,yjsr from uf_zyczbksr_xj where sjzxsr = "+id+"";
        rs.executeQuery(sql);
        List<BudgetSectionIncome2> list = new ArrayList<>();
        while (rs.next()){
            BudgetSectionIncome2 bean = getBudgetSectionIncome2();
            bean.setKmjxj(1);
            bean.setId(rs.getString("id"));

            List<BudgetSectionIncome2> xjList = recursionXj(rs.getString("id"), jzField,dw,nf);
            if (xjList.size()>0) {
                BigDecimal snjz = BigDecimal.ZERO;
                BigDecimal bnsr = BigDecimal.ZERO;
                BigDecimal snjzysap = BigDecimal.ZERO; //上年结转预算安排
                BigDecimal bnsrysap = BigDecimal.ZERO; //本年收入预算安排
                BigDecimal snjzwap = BigDecimal.ZERO; //上年结转未安排
                BigDecimal bnsrwap = BigDecimal.ZERO; //本年收入未安排
                BigDecimal snjzyszc = BigDecimal.ZERO; //上年结转预算支出
                BigDecimal bnsryszc = BigDecimal.ZERO; //本年收入预算支出
                BigDecimal snjzyszxl = BigDecimal.ZERO; //上年结转预算执行率
                BigDecimal bnsryszxl = BigDecimal.ZERO; //本年收入预算执行率
                List<String> dataidBj = new ArrayList<>();//记录本级
                List<String> dataidXj = new ArrayList<>();//记录下级
                for (BudgetSectionIncome2 beans:xjList){
                    dataidBj.add(beans.getId());
                    if (beans.getDataid() != null && !beans.getDataid().isEmpty()){
                        dataidXj.add(beans.getDataid());
                    }
                    snjz = snjz.add(new BigDecimal(beans.getSnjz()));
                    bnsr = bnsr.add(new BigDecimal(beans.getBnsr()));

                    snjzysap = snjzysap.add(new BigDecimal(beans.getSnjzysap()));
                    bnsrysap = bnsrysap.add(new BigDecimal(beans.getBnsrysap()));

                    snjzwap = snjzwap.add(new BigDecimal(beans.getSnjzwap()));
                    bnsrwap = bnsrwap.add(new BigDecimal(beans.getBnsrwap()));

                    snjzyszc = snjzyszc.add(new BigDecimal(beans.getSnjzyszc()));
                    bnsryszc = bnsryszc.add(new BigDecimal(beans.getBnsryszc()));

                    snjzyszxl = snjzyszxl.add(new BigDecimal(beans.getSnjzyszxl()));
                    bnsryszxl = bnsryszxl.add(new BigDecimal(beans.getBnsryszxl()));
                }
                String dataid = "";
                if (dataidXj.size()>0){
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",")+","+VersionFlowUtil.joinStringBySplit(dataidXj,",");
                }else{
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",");
                }
                bean.setDataid(dataid);

                bean.setSnjz(snjz.toString());
                bean.setBnsr(bnsr.toString());
                bean.setHj(snjz.add(bnsr).toString());

                bean.setSnjzysap(snjzysap.toString());
                bean.setBnsrysap(bnsrysap.toString());
                bean.setHj1(snjzysap.add(bnsrysap).toString());

                bean.setSnjzwap(snjzwap.toString());
                bean.setBnsrwap(bnsrwap.toString());
                bean.setHj2(snjzwap.add(bnsrwap).toString());

                bean.setSnjzyszc(snjzyszc.toString());
                bean.setBnsryszc(bnsryszc.toString());
                bean.setHj3(snjzyszc.add(bnsryszc).toString());

                if (snjzyszc.compareTo(new BigDecimal("0"))>0){
                    bean.setSnjzyszxl((snjzysap).divide(snjzyszc,BigDecimal.ROUND_CEILING).toString());
                }
                if (bnsryszc.compareTo(new BigDecimal("0"))>0){
                    bean.setBnsryszxl((bnsrysap).divide(bnsryszc,BigDecimal.ROUND_CEILING).toString());
                }
                if (Double.valueOf(bean.getHj3())>0){
                    bean.setHj4(new BigDecimal(bean.getHj2()).divide(new BigDecimal(bean.getHj3()),BigDecimal.ROUND_CEILING).toString());
                }
            }else{
                //已到最下级  取值
                bean.setSnjz(rs.getString(jzField));
                bean.setBnsr(rs.getString("yjsr"));
                bean.setHj(String.valueOf(new BigDecimal( rs.getString(jzField)).add(new BigDecimal(rs.getString("yjsr")))));

                bean.setSnjzysap(rs.getString("snjzhpje"));
                bean.setBnsrysap(rs.getString("yhpje"));
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));

                bean.setSnjzwap(rs.getString("yqndyjjzje"));
                bean.setBnsrwap(rs.getString("yjsr"));
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

//                String snbxje = getDfcz_tybxje("0", "1", "1", rs.getString("id"), dw, nf);
//                bean.setSnjzyszc(snbxje);

                RecordSet tempRs = new RecordSet();
                String xj = new BaseBean().getPropValue("Verification","xj")+"_"+rs.getString("id");
                BigDecimal snbxje = getSjbxje(tempRs, "上年", "0", "", "", xj);
                bean.setSnjzyszc(snbxje.toString());
                BigDecimal bnbxje = getSjbxje(tempRs, "当年", "0", "", "", xj);
                bean.setBnsryszc(bnbxje.toString());
                bean.setHj3(String.valueOf(snbxje.add(bnbxje)));

                BigDecimal snjzysap = new BigDecimal(bean.getSnjzysap());
                if (snbxje.compareTo(new BigDecimal("0"))>0&&snjzysap.compareTo(new BigDecimal("0"))>0){
                    bean.setSnjzyszxl(snjzysap.divide(snbxje,BigDecimal.ROUND_CEILING).toString());
                }
                BigDecimal bnsrysap = new BigDecimal(bean.getBnsrysap());
                if (bnbxje.compareTo(new BigDecimal("0"))>0&&bnsrysap.compareTo(new BigDecimal("0"))>0){
                    bean.setBnsryszxl(bnsrysap.divide(bnbxje,BigDecimal.ROUND_CEILING).toString());
                }
                BigDecimal hj2 = new BigDecimal(bean.getHj2());
                if (Double.valueOf(bean.getHj3())>0&&hj2.compareTo(new BigDecimal("0"))>0){
                    bean.setHj4(hj2.divide(new BigDecimal(bean.getHj3()),BigDecimal.ROUND_CEILING).toString());
                }
            }
            list.add(bean);
        }
        return list;
    }

    /**
     * 项目经费 特殊处理
     * @param nf
     * @param jz
     * @param arr
     * @return
     */
    public BudgetSectionIncome2 getXmjfData(String nf,String jz,Object[] arr){
        BudgetSectionIncome2 bean = getBudgetSectionIncome2();
        String kmdm = arr[0].toString();//编号
        String kmName = arr[1].toString();//名称
        if (!kmdm.equals("2010902") && !kmdm.equals("2010909")){
            bean.setKmmc(kmName+"-"+arr[2].toString());
        }else{
            bean.setKmmc(kmName);
        }
        bean.setName(kmdm);
        //srzx 0:人员经费 1:公用经费 2:项目经费

        RecordSet rs = new RecordSet();
        String sql = "select * from (select id,substring(gnkmmc,2,9) gnkmmc,kmdm,"+jz+",yjsr,snjzhpje,yhpje,yqndyjjzje,dw from uf_zyczbksr_km where srnf = "+nf+")kmb where gnkmmc in ("+ arr[0].toString() +")";
        rs.executeQuery(sql);
        List<String> idList = new LinkedList<>();
        while (rs.next()){
            idList.add(rs.getString("id"));
            //接下来需要累加最末级金额
            List<BudgetSectionIncome2> xjList = getCzbksrEjData(rs.getString("id"), jz,rs.getString("dw"),nf,"");
            if (kmdm.equals("2010902") || kmdm.equals("2010909")){
                //特殊处理已下一级为准
                bean.setXj(xjList);
            }else if (xjList.size()>0){
                //特殊编码之外有下级需要累加
                BigDecimal snjz = BigDecimal.ZERO;//上年结转
                BigDecimal bnsr = BigDecimal.ZERO;//本年收入
                BigDecimal snjzysap = BigDecimal.ZERO;//上年结转预算安排
                BigDecimal bnsrysap = BigDecimal.ZERO;//本年收入预算安排
                BigDecimal snjzwap = BigDecimal.ZERO; //上年结转未安排
                BigDecimal bnsrwap = BigDecimal.ZERO; //本年收入未安排
                BigDecimal snjzyszc = BigDecimal.ZERO; //上年结转预算支出
                BigDecimal bnsryszc = BigDecimal.ZERO; //本年收入预算支出
                BigDecimal snjzyszxl = BigDecimal.ZERO; //上年结转预算执行率
                BigDecimal bnsryszxl = BigDecimal.ZERO; //本年收入预算执行率
                List<String> dataidBj = new ArrayList<>();//记录本级
                List<String> dataidXj = new ArrayList<>();//记录下级
                for (BudgetSectionIncome2 beans:xjList){
                    dataidBj.add(beans.getId());
                    if (beans.getDataid() != null && !beans.getDataid().isEmpty()){
                        dataidXj.add(beans.getDataid());
                    }

                    snjz = snjz.add(new BigDecimal(beans.getSnjz()));
                    bnsr = bnsr.add(new BigDecimal(beans.getBnsr()));

                    snjzysap = snjzysap.add(new BigDecimal(beans.getSnjzysap()));
                    bnsrysap = bnsrysap.add(new BigDecimal(beans.getBnsrysap()));

                    snjzwap = snjzwap.add(new BigDecimal(beans.getSnjzwap()));
                    bnsrwap = bnsrwap.add(new BigDecimal(beans.getBnsrwap()));

                    snjzyszc = snjzyszc.add(new BigDecimal(beans.getSnjzyszc()));
                    bnsryszc = bnsryszc.add(new BigDecimal(beans.getBnsryszc()));

                    snjzyszxl = snjzyszxl.add(new BigDecimal(beans.getSnjzyszxl()));
                    bnsryszxl = bnsryszxl.add(new BigDecimal(beans.getBnsryszxl()));
                }
                String dataid = "";
                if (dataidXj.size()>0){
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",")+","+VersionFlowUtil.joinStringBySplit(dataidXj,",");
                }else{
                    dataid = VersionFlowUtil.joinStringBySplit(dataidBj,",");
                }
                bean.setDataid(dataid);

                bean.setSnjz(new BigDecimal(bean.getSnjz()).add(snjz).toString());
                bean.setBnsr(new BigDecimal(bean.getBnsr()).add(bnsr).toString());
                bean.setHj(String.valueOf(new BigDecimal(bean.getSnjz()).add(new BigDecimal(bean.getBnsr()))));

                bean.setSnjzysap(new BigDecimal(bean.getSnjzysap()).add(snjzysap).toString());
                bean.setBnsrysap(new BigDecimal(bean.getBnsrysap()).add(bnsrysap).toString());
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));

                bean.setSnjzwap(new BigDecimal(bean.getSnjzwap()).add(snjzwap).toString());
                bean.setBnsrwap(new BigDecimal(bean.getBnsrwap()).add(bnsrwap).toString());
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

                bean.setSnjzyszc(new BigDecimal(bean.getSnjzyszc()).add(snjzyszc).toString());
                bean.setBnsryszc(new BigDecimal(bean.getBnsryszc()).add(bnsryszc).toString());
                bean.setHj3(String.valueOf(new BigDecimal(bean.getSnjzyszc()).add(new BigDecimal(bean.getBnsryszc()))));

                if (snjzyszc.compareTo(new BigDecimal("0"))>0){
                    bean.setSnjzyszxl((snjzysap).divide(snjzyszc,BigDecimal.ROUND_CEILING).toString());
                }
                if (bnsryszc.compareTo(new BigDecimal("0"))>0){
                    bean.setBnsryszxl((bnsrysap).divide(bnsryszc,BigDecimal.ROUND_CEILING).toString());
                }
                if (Double.valueOf(bean.getHj3())>0){
                    bean.setHj4(new BigDecimal(bean.getHj2()).divide(new BigDecimal(bean.getHj3()),BigDecimal.ROUND_CEILING).toString());
                }
            }else{
                //已到最下级  取自身值
                String snjz = new BigDecimal(bean.getSnjz()).add(new BigDecimal(rs.getString(jz))).toString();
                bean.setSnjz(snjz);
                String yjsr = new BigDecimal(bean.getSnjz()).add(new BigDecimal(rs.getString("yjsr"))).toString();
                bean.setBnsr(yjsr);
                bean.setHj(String.valueOf(new BigDecimal(snjz).add(new BigDecimal(yjsr))));

                bean.setSnjzysap(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(rs.getString("snjzhpje"))).toString());
                bean.setBnsrysap(new BigDecimal(bean.getBnsrysap()).add(new BigDecimal(rs.getString("yhpje"))).toString());
                bean.setHj1(String.valueOf(new BigDecimal(bean.getSnjzysap()).add(new BigDecimal(bean.getBnsrysap()))));

                bean.setSnjzwap(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(rs.getString("yqndyjjzje"))).toString());
                bean.setBnsrwap(new BigDecimal(bean.getBnsrwap()).add(new BigDecimal(rs.getString("yjsr"))).toString());
                bean.setHj2(String.valueOf(new BigDecimal(bean.getSnjzwap()).add(new BigDecimal(bean.getBnsrwap()))));

                RecordSet tempRs = new RecordSet();
                String km = new BaseBean().getPropValue("Verification","yj")+"_"+rs.getString("id");
                BigDecimal snbxje = getSjbxje(tempRs, "上年", "0", "", "", km);
                bean.setSnjzyszc(snbxje.toString());
                BigDecimal bnbxje = getSjbxje(tempRs, "当年", "0", "", "", km);
                bean.setBnsryszc(bnbxje.toString());
                bean.setHj3(String.valueOf(snbxje.add(bnbxje)));

                BigDecimal snjzysap = new BigDecimal(bean.getSnjzysap());
                if (snbxje.compareTo(new BigDecimal("0"))>0&&snjzysap.compareTo(new BigDecimal("0"))>0){
                    bean.setSnjzyszxl(snjzysap.divide(snbxje,BigDecimal.ROUND_CEILING).toString());
                }
                BigDecimal bnsrysap = new BigDecimal(bean.getBnsrysap());
                if (bnbxje.compareTo(new BigDecimal("0"))>0&&bnsrysap.compareTo(new BigDecimal("0"))>0){
                    bean.setBnsryszxl(bnsrysap.divide(bnbxje,BigDecimal.ROUND_CEILING).toString());
                }
                BigDecimal hj2 = new BigDecimal(bean.getHj2());
                if (Double.valueOf(bean.getHj3())>0&&hj2.compareTo(new BigDecimal("0"))>0){
                    bean.setHj4(hj2.divide(new BigDecimal(bean.getHj3()),BigDecimal.ROUND_CEILING).toString());
                }
            }
        }
        bean.setId(VersionFlowUtil.joinStringBySplit(idList,","));
        return bean;
    }




    /**
     * 获取地方财政同意报销金额
     * @param zcqd
     *          0和1 0为财政拨款 1为非财政拨款
     * @param ysly
     *          0和1 0为当年预算 1为上年结转
     * @param kmjxj
     *          0和1 分别对应科目级和科目下级，在财政收入的层级里0就是第一级，非第一级的都为1
     * @param dygnkmbid
     *          当kmjxj为0时为uf_zyfzbksr_km表的id，1则是uf_zyfzbksr_xj表的id
     * @param dw
     *          单位
     * @param nf
     *          年份
     * @return
     */
    public String getDfcz_tybxje(String zcqd,String ysly,String kmjxj,String dygnkmbid,String dw,String nf){
        RecordSet rs = new RecordSet();
        String sql = "select ISNULL(SUM(d1.tybxje-isnull(d1.cxje,0)),0) AS tybxje  from uf_bxjl_kj_dt1 where zcqd="+zcqd+" and ysly="+ysly+"  and kmjxj="+kmjxj+" and dygnkmbid='"+dygnkmbid+"' and ysxm in (select lcjlsqjlid from uf_jingfei_version where dlhsdw="+dw+" and ysnd="+nf+") ";
        rs.executeQuery(sql);
        if (rs.next()){
            if (rs.getString("tybxje") == null || rs.getString("tybxje").equals(""))
                return "0";
            else
                return rs.getString("tybxje");
        }else{
            return "0";
        }
    }

    /**
     * 查询渠道实际报销金额
     * @param yslylx 预算来源（当年 or 上年）
     * @param srlx  收入类型（财政 or 非财政）
     * @param srfl 非中央收入分类
     * @param dfczbkxm 地方财政拨款项目id
     * @param zyczbksr  中央财政核批id
     * @return
     */
    public BigDecimal getSjbxje(RecordSet rs, String yslylx, String srlx, String srfl, String dfczbkxm, String zyczbksr){
        String baseSql = "SELECT ISNULL(SUM(d1.tybxje-isnull(d1.cxje,0)),0) AS sjbxje FROM dbo.uf_bxjl_kj_dt1 d1 \n" +
                "LEFT JOIN uf_bxjl_kj d ON d1.lcid=d.lcid\n" +
                "LEFT JOIN dbo.workflow_requestbase wr ON d1.lcid=wr.requestid\n" +
                "LEFT JOIN workflow_base wb ON wr.workflowid=wb.id\n" +
                "WHERE  wr.currentnodetype=3 and ysly="+(yslylx.startsWith("当年")?"0":"1")+" and zcqd="+srlx+" \n";
        if(Integer.parseInt(srlx)==0){
            //财政拨款
            String kmj = new BaseBean().getPropValue("Verification","yj");
            if(zyczbksr.split("_")[0].equals(kmj)){
                baseSql += " and kmjxj=0 and dygnkmbid="+zyczbksr.split("_")[1];
            }else{
                baseSql += " and kmjxj=1 and dygnkmbid="+zyczbksr.split("_")[1];
            }
        }else{
            if(srfl.equals("5") || srfl.equals("8") ){
                baseSql += " and fzysrfl="+srfl+" and zcdfczbkxm="+dfczbkxm;
            }else {
                baseSql += " and fzysrfl=" + srfl ;
            }
        }
        System.err.println(baseSql);
        rs.execute(baseSql);
        if(rs.next()){
            return new BigDecimal(rs.getString("sjbxje"));
        }
        return new BigDecimal("0.00");
    }

    private BudgetSectionIncome2 getBudgetSectionIncome2(){
        BudgetSectionIncome2 bean = new BudgetSectionIncome2();
        bean.setSnjz("0.00");
        bean.setBnsr("0.00");
        bean.setHj("0.00");

        bean.setSnjzysap("0.00");
        bean.setBnsrysap("0.00");
        bean.setHj1("0.00");

        bean.setSnjzwap("0.00");
        bean.setBnsrwap("0.00");
        bean.setHj2("0.00");

        bean.setSnjzyszc("0.00");
        bean.setBnsryszc("0.00");
        bean.setHj3("0.00");

        bean.setSnjzyszxl("0.00");
        bean.setBnsryszxl("0.00");
        bean.setHj4("0.00");

        return bean;
    }
}
